package com.ztmg.water.service.apartment.impl;

import com.ztmg.common.util.DateUtil;
import com.ztmg.water.dto.apartment.ApartmentInfoDTO;
import com.ztmg.water.mapper.apartment.ApartmentInfoMapper;
import com.ztmg.water.mapper.apartment.ApartmentRoomsMapper;
import com.ztmg.water.model.apartment.ApartmentFloor;
import com.ztmg.water.model.apartment.ApartmentRooms;
import com.ztmg.water.service.apartment.ApartmentInfoService;
import com.ztmg.water.vo.apartment.ApartmentFloorVO;
import com.ztmg.water.dto.apartment.ApartmentFloorDTO;
import com.ztmg.water.dto.apartment.ApartmentFloorPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.apartment.ApartmentFloorService;
import com.ztmg.water.mapper.apartment.ApartmentFloorMapper;import com.ztmg.water.model.apartment.ApartmentFloorExample;

@Service
public class ApartmentFloorServiceImpl implements ApartmentFloorService{
    @Resource
    private ApartmentInfoMapper  apartmentInfoMapper;
    @Resource
    private ApartmentFloorMapper apartmentFloorMapper;
    @Resource
    private ApartmentRoomsMapper  apartmentRoomsMapper;

    @Override
    public JsonResult createApartmentFloor(ApartmentFloorDTO dto) {
        if(null != dto){
            ApartmentFloor bean = new ApartmentFloor();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            apartmentFloorMapper.insert(bean);
            int floorId = bean.getId();
            int rooms = bean.getFloorRooms();
            Format f3 = new DecimalFormat("000");
            //每创建一层增加当前公寓的层数
            for (int i=0;i<rooms;i++)
            {
                ApartmentRooms roomsBean = new ApartmentRooms();
                roomsBean.setApartmentId(bean.getApartmentId());
                roomsBean.setCreateTime(new Date());
                roomsBean.setFloorId(floorId);
                roomsBean.setModifyTime(new Date());
                roomsBean.setRemark(bean.getFloorSn()+"-"+f3.format(i+1));
                roomsBean.setRoomName(bean.getFloorSn()+"-"+f3.format(i+1));
                roomsBean.setRoomNumber(String.valueOf(i+1));
                roomsBean.setRoomSn(bean.getFloorSn()+"-"+f3.format(i+1));
                apartmentRoomsMapper.insert(roomsBean);
            }

            //修改当前公寓楼层数量
            ApartmentInfoDTO paramsObj = new ApartmentInfoDTO();
            paramsObj.setId(bean.getApartmentId());
            paramsObj.setApartmentFloors(1);
            paramsObj.setModifier("");
            paramsObj.setModifyTime(DateUtil.parseDateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
            apartmentInfoMapper.addFloorNumsById(paramsObj);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }


    @Override
    public JsonResult create(ApartmentFloorDTO dto) {
        if(null != dto){
            ApartmentFloor bean = new ApartmentFloor();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            apartmentFloorMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(ApartmentFloorDTO dto) {
        if(null != dto && null != dto.getId()){
            ApartmentFloor bean = new ApartmentFloor();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            apartmentFloorMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<ApartmentFloorVO> selectById(Integer id) {
        if(null != id){
            ApartmentFloor bean = apartmentFloorMapper.selectByPrimaryKey(id);
            ApartmentFloorVO vo = new ApartmentFloorVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<ApartmentFloorVO>> select(ApartmentFloorDTO dto) {
        ApartmentFloorExample example = new ApartmentFloorExample();
        ApartmentFloorExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<ApartmentFloor> beanList = apartmentFloorMapper.selectByExample(example);
        List<ApartmentFloorVO> respDTOS = beanList.parallelStream().map((bean) -> {
            ApartmentFloorVO vo = new ApartmentFloorVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<ApartmentFloorVO>> selectPage(ApartmentFloorPageDTO dto) {
        ApartmentFloorExample example = new ApartmentFloorExample();
        ApartmentFloorExample.Criteria criteria =  example.createCriteria();
        if(dto.getApartmentId()!=null)
        {
            criteria.andApartmentIdEqualTo(dto.getApartmentId());
        }

        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<ApartmentFloor> beanList = apartmentFloorMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<ApartmentFloorVO> respDTOS = beanList.parallelStream().map((bean) -> {
            ApartmentFloorVO vo = new ApartmentFloorVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        //修改当前公寓楼层数量
        ApartmentInfoDTO paramsObj = new ApartmentInfoDTO();
        paramsObj.setId(id);
        paramsObj.setApartmentFloors(1);
        paramsObj.setModifier("");
        paramsObj.setModifyTime(DateUtil.parseDateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
        apartmentInfoMapper.decreaseFloorNumsById(paramsObj);
        return JsonResult.success( apartmentFloorMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(ApartmentFloorDTO dto) {
        ApartmentFloorExample example = new ApartmentFloorExample();
        ApartmentFloorExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(apartmentFloorMapper.deleteByExample(example));
    }
}